package com.example.demo.service.userInfo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.domain.role.Role;
import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.domain.userInfo.UserRoleKey;
import com.example.demo.mapper.role.RoleMapper;
import com.example.demo.mapper.userInfo.UserInfoMapper;
import com.example.demo.mapper.userInfo.UserRoleMapper;
import com.example.demo.service.PasswordService;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Autowired
	private PasswordService passwordService;

	@Override
	public UserInfo findByUsername(String username) {
		// TODO Auto-generated method stub
		return this.userInfoMapper.findByUsername(username);
	}

	@Override
	public int deleteByPrimaryKey(Integer uid) {
		// TODO Auto-generated method stub
		// 删除用户角色关联
		userRoleMapper.deleteUserRoleByUserId(uid);

		return this.userInfoMapper.deleteByPrimaryKey(uid);
	}

	@Override
	public int insert(UserInfo record) {
		// TODO Auto-generated method stub
		record.randomSalt();
		record.setPassword(passwordService.encryptPassword(record.getPassword(), record.getSalt()));
		int rows = userInfoMapper.insert(record);
		// 新增用户岗位关联

		// 新增用户与角色管理
		insertUserRole(record);
		return rows;
	}

	private void insertUserRole(UserInfo user) {
		// TODO Auto-generated method stub
		Integer[] roles = user.getRoleIds();
		if (roles.length != 0) {
			List<UserRoleKey> list = new ArrayList<UserRoleKey>();
			for (Integer roleId : user.getRoleIds()) {
				UserRoleKey ur = new UserRoleKey();
				ur.setUid(user.getUid());
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleMapper.batchUserRole(list);
			}
		}
	}

	@Override
	public int insertSelective(UserInfo record) {
		// TODO Auto-generated method stub
		return this.userInfoMapper.insertSelective(record);
	}

	@Override
	public UserInfo selectByPrimaryKey(Integer uid) {
		// TODO Auto-generated method stub
		return this.userInfoMapper.selectByPrimaryKey(uid);
	}

	@Override
	public int updateByPrimaryKeySelective(UserInfo user) {
		// TODO Auto-generated method stub
		Integer userId = user.getUid();

		// 删除用户与角色关联
		userRoleMapper.deleteUserRoleByUserId(userId);
		// 新增用户与角色管理
		insertUserRole(user);
		return this.userInfoMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public int updateByPrimaryKey(UserInfo user) {
		// TODO Auto-generated method stub

		return userInfoMapper.updateByPrimaryKey(user);
	}

	@Override
	public List<UserInfo> selectUserList(UserInfo user) {
		// TODO Auto-generated method stub
		return this.userInfoMapper.selectUserList(user);
	}

	@Override
	public UserInfo selectUserByUserName(String userName) {
		// TODO Auto-generated method stub
		return this.userInfoMapper.selectUserByUserName(userName);
	}

	/**
	 * 用户名唯一性检验
	 */
	@Override
	public int checkUserNameUnique(String userName) {
		// TODO Auto-generated method stub
		return userInfoMapper.checkUserNameUnique(userName);
	}

	/**
	 * 批量删除
	 */
	@Override
	public int deleteUserByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		String[] id = ids.split(",");
		Integer[] userIds = new Integer[id.length];
		for (Integer userId : userIds) {
			if (UserInfo.isAdmin(userId)) {
				throw new Exception("不允许删除超级管理员用户");
			}
		}
		// 删除用户角色关联
		for (int i = 0; i < id.length; i++) {
			Integer v = Integer.parseInt(id[i]);
			userRoleMapper.deleteUserRoleByUserId(v);
		}
		return userInfoMapper.deleteUserByIds(id);
	}

	/**
	 * 查询用户所属角色组
	 *
	 * @param userId
	 *            用户ID
	 * @return 结果
	 */
	@Override
	public String selectUserRoleGroup(Integer uid) {
		// TODO Auto-generated method stub
		List<Role> list = roleMapper.selectRolesByUserId(uid);
		StringBuffer idsStr = new StringBuffer();
		for (Role role : list) {
			idsStr.append(role.getRoleName()).append(",");
		}
		if (idsStr.toString() != null) {
			return idsStr.substring(0, idsStr.length() - 1);
		}
		return idsStr.toString();
	}

	@Override
	public int resetUserPwd(UserInfo user) {
		// TODO Auto-generated method stub
		user.randomSalt();
		user.setPassword(passwordService.encryptPassword(user.getPassword(), user.getSalt()));
		return userInfoMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public int changeState(UserInfo user) throws Exception {
		// TODO Auto-generated method stub
		if (UserInfo.isAdmin(user.getUid())) {
			throw new Exception("不允许修改超级管理员用户");
		}
		return userInfoMapper.updateByPrimaryKeySelective(user);
	}

}
